import uuid

from django.db import models
from pgcrypto import fields

from apps.core.models import GenericModel
from apps.tenant.types import (
    ProviderLocationIcon, PROVIDER_LOCATION_ICON, PROVIDER_LOCATION_PLACE_OF_SERVICES, ProviderLocationPlaceOfServices,
    PROVIDER_TELEHEALTH_TYPE, ProviderTelehealthType, PROVIDER_WEEK_START, ProviderWeekStart)

from localflavor.us.models import (
    USStateField,
    USZipCodeField,
)


def upload_invoice_logo_image(instance, filename):
    path = "settings/invoice/image/{provider_id}/{filename}".format(
        provider_id=instance.provider.id, filename=filename
    )
    return path


def upload_super_bill_logo_image(instance, filename):
    path = "settings/superbill/image/{provider_id}/{filename}".format(
        provider_id=instance.provider.id, filename=filename
    )
    return path


class TrackingDeviceLogin(models.Model):
    device_id = models.CharField(max_length=255, unique=True)
    created_at = models.DateTimeField(auto_now=True,
                                      null=True,
                                      editable=False,
                                      verbose_name=u'Updated at', )
    updated_at = models.DateTimeField(auto_now=True,
                                      null=True,
                                      editable=False,
                                      verbose_name=u'Updated at', )

    class Meta:
        db_table = "tracking_device_login"
        indexes = [models.Index(fields=['device_id'])]


class ProviderLocation(GenericModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    name = models.CharField(max_length=255, blank=True)
    address = models.CharField(
        null=True, blank=True, max_length=255)
    state = USStateField(null=True, blank=True)
    city = models.CharField(max_length=50, null=True, blank=True)
    zipcode = USZipCodeField(null=True, blank=True)
    icon = models.IntegerField(choices=PROVIDER_LOCATION_ICON,
                               default=ProviderLocationIcon.OFFICE.value
                               )
    place_of_service = models.IntegerField(choices=PROVIDER_LOCATION_PLACE_OF_SERVICES,
                                           default=ProviderLocationPlaceOfServices.OFFICE.value)
    is_default = models.BooleanField(default=False)

    class Meta:
        db_table = 'provider_location'


class ProviderServices(GenericModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    code = fields.CharPGPSymmetricKeyField(max_length=50, null=True, blank=True)
    service = fields.CharPGPSymmetricKeyField(max_length=255, null=True, blank=True)
    time = models.IntegerField(default=0)
    fee = models.IntegerField(default=0)

    class Meta:
        db_table = 'provider_services'


class ProviderTelehealth(GenericModel):
    telehealth_type = models.IntegerField(choices=PROVIDER_TELEHEALTH_TYPE,
                                          default=ProviderTelehealthType.NONE.value)
    access_token = models.TextField(null=True, blank=True)
    refresh_token = models.TextField(null=True, blank=True)

    class Meta:
        db_table = 'provider_telehealth'


class Calendar(models.Model):
    start_time = models.TimeField()
    end_time = models.TimeField()
    calendar_sync = models.BooleanField(default=True)
    week_started = models.IntegerField(choices=PROVIDER_WEEK_START,
                                    default=ProviderWeekStart.MONDAY.value)

    class Meta:
        db_table = 'calendar'


class BillingAddress(GenericModel):
    name = models.CharField(max_length=500, null=True, blank=True)
    address = fields.TextPGPSymmetricKeyField(null=True)

    class Meta:
        db_table = 'billing_address'


class InvoiceSetting(GenericModel):
    include_logo = models.BooleanField(default=True)
    footer_information = models.TextField(null=True)
    logo_image = models.ImageField(upload_to=upload_invoice_logo_image, blank=True, null=True)
    subject = models.TextField(null=True)
    message = models.TextField(null=True)
    email = fields.EmailPGPSymmetricKeyField(null=True)

    class Meta:
        db_table = 'invoice_setting'


class SuperBillSetting(GenericModel):
    include_signature_line = models.BooleanField(default=True)
    include_diagnosis_description = models.BooleanField(default=True)
    include_logo = models.BooleanField(default=True)
    logo_image = models.ImageField(upload_to=upload_super_bill_logo_image, blank=True, null=True)
    footer_information = models.TextField(null=True)
    subject = models.TextField(null=True)
    message = models.TextField(null=True)
    email = fields.EmailPGPSymmetricKeyField(null=True)

    class Meta:
        db_table = 'superbill_setting'


class BillingInfo(GenericModel):
    ssn = fields.CharPGPSymmetricKeyField(max_length=255, null=True)
    npi = fields.CharPGPSymmetricKeyField(max_length=255, null=True)
    organization_npi = fields.CharPGPSymmetricKeyField(max_length=255, null=True)

    class Meta:
        db_table = 'billing_info'
